package com.pinda.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pinda.base.entity.PdTransportTrips;
import com.pinda.base.entity.PdTransportTripsTruckDriver;
import com.pinda.base.transportline.TransportTripsDto;
import com.pinda.base.transportline.TransportTripsTruckDriverDto;
import com.pinda.feign.AggregationFeign;
import com.pinda.feign.AggregationTripsFeign;
import com.pinda.service.IPdTransportTripsService;
import com.pinda.service.IPdTransportTripsTruckDriverService;
import jdk.internal.dynalink.linker.LinkerServices;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 车次信息表 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd_transport_trips")
public class PdTransportTripsController {

    @Autowired
    IPdTransportTripsService pdTransportTripsService;


    @Autowired
    AggregationTripsFeign aggregationTripsFeign;

    @Autowired
    IPdTransportTripsTruckDriverService iPdTransportTripsTruckDriverService;

    /**
     * 获取车次与车辆和司机关联关系列表
     *
     * @param transportTripsId 车次id
     * @param truckId          车辆id
     * @param userId           司机id
     * @return 车次与车辆和司机关联关系列表
     */
    @GetMapping("/truckDriver")
    public List<TransportTripsTruckDriverDto> findAllTruckDriverTransportTrips(@RequestParam(name = "transportTripsId", required = false) String transportTripsId, @RequestParam(name = "truckId", required = false) String truckId, @RequestParam(name = "userId", required = false) String userId) {
        return iPdTransportTripsTruckDriverService.findAll(transportTripsId, truckId, userId).stream().map(pdTransportTripsTruck -> {
            TransportTripsTruckDriverDto dto = new TransportTripsTruckDriverDto();
            BeanUtils.copyProperties(pdTransportTripsTruck, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    /**
     * 获取车次列表
     *
     * @param transportLineId 线路id
     * @param ids             车次id列表
     * @return 车次列表
     */
    @GetMapping("")
    public List<TransportTripsDto> findAll(@RequestParam(name = "transportLineId", required = false) String transportLineId, @RequestParam(name = "ids", required = false) List<String> ids) {
        return pdTransportTripsService.findAll(transportLineId, ids).stream().map(pdTransportTrips -> {
            TransportTripsDto dto = new TransportTripsDto();
            BeanUtils.copyProperties(pdTransportTrips, dto);
            return dto;
        }).collect(Collectors.toList());
    }



    /**
     * 根据id获取车次详情
     *
     * @param id 车次id
     * @return 车次信息
     */
    @GetMapping("/{id}")
    public TransportTripsDto fineById(@PathVariable(name = "id") String id) {
        PdTransportTrips pdTransportTrips = pdTransportTripsService.getById(id);
        TransportTripsDto dto = new TransportTripsDto();
        if (pdTransportTrips != null) {
            BeanUtils.copyProperties(pdTransportTrips, dto);
        }else{
            dto.setId(id);
        }
        return dto;
    }
    /**
     * 添加车次
     * @return
     */
    @PostMapping("/addPdTransportTrips")
    public Boolean addPdTransportTrips(@RequestBody PdTransportTrips pdTransportTrips){
        pdTransportTrips.setId("12358");
        this.pdTransportTripsService.save(pdTransportTrips);
        PdTransportTripsTruckDriver pdTransportTripsTruckDriver = new PdTransportTripsTruckDriver();
        pdTransportTripsTruckDriver.setId("555555");
        pdTransportTripsTruckDriver.setTransportTripsId(pdTransportTrips.getId());
        return this.iPdTransportTripsTruckDriverService.save(pdTransportTripsTruckDriver);
    }

    @GetMapping("/queryTransportTripsByTransportLineId/{TransportLineId}")
    public List queryTransportTripsByTransportLineId(@PathVariable("TransportLineId") String TransportLineId){
        return this.aggregationTripsFeign.queryTransportTripsByTransportLineId(TransportLineId);
    }



    @PostMapping("/updateTransportTripsBy")
    public Boolean updateTransportTripsBy(@RequestBody PdTransportTrips pdTransportTrips){


        boolean b = this.pdTransportTripsService.updateById(pdTransportTrips);

        return b;
    }


    @PostMapping("/deleteTransportTripsById/{id}/{tttpid}")
    public Boolean deleteTransportTripsBy(@PathVariable("id") String id,@PathVariable("tttpid") String tttpid){
        QueryWrapper<PdTransportTripsTruckDriver> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id",tttpid);
        this.iPdTransportTripsTruckDriverService.remove(objectQueryWrapper);
            this.pdTransportTripsService.removeById(id);

        return false;
    }



}
